<template>
  <div class="todo-footer" v-show="totalTodo">
    <label>
      <!-- <input type="checkbox" :checked="isAll" @change="checkAll" /> -->
      <!-- 另一种写法 -->
      <input type="checkbox" v-model="isAll" />
    </label>
    <span>
      <span>已完成{{ doneTodo }}</span> / 全部{{ totalTodo }}
    </span>
    <button class="btn btn-danger" @click="handleAllDone">清除已完成任务</button>
  </div>
</template>

<script>
export default {
  name: "TodoFooter",
  props: ["todos", "checkAllTodo",'deleteAllDone'],
  computed: {
    doneTodo() {
      return this.todos.reduce((pre, current) => {
        //   console.log('@@',pre);
        // pre 是上一次第一个参数的函数的返回值，最后一个返回值作为最终reduce的返回值
        return pre + (current.done ? 1 : 0);
      }, 0);
    },
    totalTodo() {
      return this.todos.length;
    },
    // isAll() {
    //   //   计算属性不是函数！而是函数的返回值，函数是简写形式
    //   return this.doneTodo === this.totalTodo && this.total !== 0;
    // },
    isAll: {
      get() {
        return this.doneTodo === this.totalTodo && this.total !== 0;
      },
      set(val) {
        this.checkAllTodo(val);
      },
    },
  },
  methods: {
    // checkAll(e) {
    //   //   console.log(e.target.checked);
    //   this.checkAllTodo(e.target.checked);
    // },
    handleAllDone(){
        this.deleteAllDone()
    }
  },
};
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>